local skill = fk.CreateSkill {
  name = "#fhyx_moon_spear_skill",
  attached_equip = "fhyx_moon_spear",
}


skill:addEffect(fk.TurnEnd, {
  can_trigger = function(self, event, target, player, data)
    -- 只在其他角色回合结束时触发
    if player == target then return false end
    if not player:hasSkill(self.name) then return false end
    -- 检查本回合是否失去过手牌
    if player:getMark("_moon_spear_lost_hand") ~= 1 then return false end
    -- 检查是否有目标
    local room = player.room
    local targets = {}
    for _, p in ipairs(room.alive_players) do
      if p ~= player and player:isInAttackRange(p) and p:getMark("_moon_spear_got_hand") == 1 then
        table.insert(targets, p)
      end
    end
    return #targets > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    -- 找到可选目标
    local targets = {}
    for _, p in ipairs(room.alive_players) do
      if p ~= player and player:isInAttackRange(p) and p:getMark("_moon_spear_got_hand") == 1 then
        table.insert(targets, p.id)
      end
    end
    if #targets == 0 then return false end
    local to = room:askForChoosePlayer(player, targets, "#moon_spear_skill2-ask", 1, 1)
    if to and #to > 0 then
      local use = room:askToUseCard(player, {
        skill_name = "#moon_spear_skill2-ask",
        pattern = "slash",
        to = to[1],
        cancelable = true
      })
      if use then
        event:setCostData(self, {use = use})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setEmotion(player, "./packages/overseas/image/anim/moon_spear")
    room:useCard(event:getCostData(self).use)
  end,
})

-- 记录本回合失去过手牌
skill:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self.name) then return false end
    if player.room.current ~= player then return false end
    for _, move in ipairs(data) do
      if move.from == player and move.fromArea == Card.PlayerHand then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_moon_spear_lost_hand", 1)
  end,
})

-- 记录本回合获得过手牌
skill:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
    if player.room.current ~= player then return false end
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_moon_spear_got_hand", 1)
  end,
})

-- 回合结束时清除标记
skill:addEffect(fk.TurnStart, {
  can_trigger = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_moon_spear_lost_hand", 0)
    player.room:setPlayerMark(player, "_moon_spear_got_hand", 0)
  end,
})

Fk:loadTranslationTable{
  ["zyg_am__moon_spear"] = "银月枪",
  [":zyg_am__moon_spear"] = "装备牌·武器<br /><b>攻击范围</b>：３<br /><b>武器技能</b>：一名其他角色回合结束时，若你于此回合失去过手牌，则你可以对攻击范围内的一名本回合获得过手牌的角色使用一张【杀】。",
  ["#fhyx_moon_spear_skill"] = "银月枪",
  ["#fhyx_moon_spear_skill-ask"] = "银月枪：你可以使用一张【杀】",
  [":fhyx_moon_spear_skill"] = "银月枪",
}

return skill